package com.iptv.grape.event;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.iptv.grape.BaseActivity;
import com.iptv.grape.event.JSRequest;
import com.iptv.grape.event.factory.BuildEventProc;
import com.iptv.grape.event.proc.IWebViewMessageProc;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.video.XMediaPlayer;
// WebView接收H5请求的通信
public class DefBridgeHandler implements BridgeHandler {
    private BaseActivity baseActivity;
    private XMediaPlayer mediaPlayer;

    public DefBridgeHandler(BaseActivity activity, XMediaPlayer mediaPlayer){
        this.baseActivity = activity;
        this.mediaPlayer = mediaPlayer;
    }

    @Override
    public void handler(String data, CallBackFunction function) {
        AppLog.getCurLog().out("收到H5的消息：" + data);
        String callBackJson = "";
        JSRequest jsRequest = JSRequest.Build(data);
        if(jsRequest != null){
            try {
                IWebViewMessageProc proc = BuildEventProc.Build(jsRequest);
                proc.init(jsRequest);
                callBackJson = proc.procMessage(this.baseActivity,this.mediaPlayer);
            }
            catch (Exception e){
                e.printStackTrace();
                AppLog.getCurLog().out(e.getMessage());
            }
        }
        function.onCallBack(callBackJson);
        //AppLog.getCurLog().out(callBackJson);
    }
}
